package com.itheima.controller;

import com.itheima.common.DefaultException;
import com.itheima.common.Paging;
import com.itheima.common.R;
import com.itheima.dto.NoticeDto;
import com.itheima.dto.PageNoticeDto;
import com.itheima.dto.UpdateNoticeDto;
import com.itheima.service.NoticeService;
import com.itheima.vo.NoticePersonVo;
import com.itheima.vo.NoticeVo;
import com.itheima.vo.organization.OrganizationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * <h1>通知控制类</h1>
 *
 * @author <a href="515857087@qq.com">叶世铭</a>
 * @since 2022/11/3
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "通知api")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    /**
     * 发送通知
     */
    @ApiOperation(("发送通知"))
    @PostMapping("/sendNotice")
    public R<String> sendNotice(@RequestBody @Validated NoticeDto noticeDto) throws Exception {
        noticeService.sendNotice(noticeDto);
        return R.success("通知已发送");
    }

    /**
     * 确认收到通知
     */
    @ApiOperation("确认收到通知")
    @PutMapping("/acceptNotice")
    public R<String> acceptNotice(@RequestParam("noticeId") Integer noticeId) throws Exception {
        noticeService.acceptNotice(noticeId);
        return R.success("已确认");
    }

    /**
     * 更新通知
     *
     * @param updateNoticeDto
     * @return
     */
    @ApiOperation("更新通知")
    @PutMapping("/updateNotice")
    public R<String> updateNotice(@RequestBody @Validated UpdateNoticeDto updateNoticeDto) throws Exception {
        noticeService.updateNotice(updateNoticeDto);
        return R.success("通知更新成功");
    }

    /**
     * 删除通知（删除自己所发布的）
     *
     * @param noticeId
     * @return
     */
    @ApiOperation(value = "删除通知")
    @DeleteMapping("/deleteNotice")
    public R<String> deleteNotice(@NotNull @RequestParam("noticeId") Integer noticeId) throws Exception {
        noticeService.deleteNotice(noticeId);
        return R.success("删除成功");
    }

    /**
     * 分页展示通知信息（自己发布的）
     */
    @ApiOperation(value = "分页展示通知信息（自己发布的）")
    @PostMapping("/pageNotice")
    public R<Paging<NoticeVo>> pageNotice(@RequestBody PageNoticeDto pageNoticeDto) throws Exception {
        return R.success(noticeService.pageNotice(pageNoticeDto));
    }

    /**
     * 分页展示通知信息（需要处理的）
     */
    @ApiOperation(value = "分页展示通知信息（需要处理的）")
    @PostMapping("/pageNoticePerson")
    public R<Paging<NoticePersonVo>> pageNoticePerson(@Validated @RequestBody PageNoticeDto pageNoticeDto) throws Exception {
        return R.success(noticeService.pageNoticePerson(pageNoticeDto));
    }

    /**
     * 展示可通知的用户（按层级展示）
     */
    @ApiOperation(value = "展示可通知的用户（按层级展示）")
    @GetMapping("/showUser")
    public R<OrganizationVo> showUser(@NotNull @RequestParam("organizationId") Integer organizationId) throws DefaultException {
        return R.success(noticeService.showUser(organizationId));
    }
}
